$(function(){
    //获取url参数
	function GetQueryString(name){
		var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if(r!=null)return unescape(r[2]); return null;
    }
    //显示遮罩层
    dormitory_view=function(tabel,student_id){
        $("#modal-dormitory-view").modal("show");
        $("#dormitory_student_id").val(student_id);
        $("#dormitory_gender").val($(tabel).parents("tr").find(".gender").html());
    }
    //分配宿舍
    allocatingDormitory=function(){
        flag=0;
        allocateList=new Array();
        $(":checkbox[name='dormitory_checkbox']:checked").each(function(index,e){
            beds=$(this).parents("tr").find(".beds").html();
            property=$(this).parents("tr").find(".property").html();
            gender=$("#dormitory_gender").val()+"生宿舍";

            if(gender==property){
                if(beds>0){
                    dormitory=new Object();
                    dormitory.student_id= $("#dormitory_student_id").val();
                    dormitory.apartment_id=$(this).val();
                    dormitory.floor=$(this).parents("tr").find(".floor").html();
                    dormitory.dormitory_id=$(this).parents("tr").find(".dormitory_id").html();
                    dormitory.action=1;
                    allocateList[index]=dormitory;
                }else{
                    //layer.msg('不能分配，没有空床位');
                    flag=1;
                }
            }else{
                //layer.msg('性别不匹配');
                flag=2;
            }
            
        });
        switch(flag){
            case 0:
                if(allocateList.length==1){
                    allocateJSON=JSON.stringify(allocateList);
                    $.post("/php/student-dormitory-allocating.php",{action:1,data:allocateJSON},
                    function(result){
                        resultObj=JSON.parse(result);
                        //alert(result);
                        switch(resultObj.code){
                            case '2001':
                                layer.msg(resultObj.message,{icon:1,btn:['确定']},function(){
                                    location.replace(location.href);
                                });
                                break;
                            case '2002':layer.msg(resultObj.message);break;
                            case '2003':layer.msg(resultObj.message);break;
                            case '2004':layer.msg(resultObj.message);break;
                        }
                    });
                }else if(allocateList.length==0){
                    layer.msg('未选中任何项');
                }else{
                    layer.msg('只能选择一个宿舍');
                }
            break;
            case 1:layer.msg('不能分配，没有空床位');break;
            case 2:layer.msg('性别不匹配');break;
            default:break;
        }
    }
    //移除选中
    removeStudentDormitory=function(){
        removeList=new Array();
        $(":checkbox[name='student_checkbox']:checked").each(function(index,e){
            allocate_status=$(this).parents("tr").find(".allocate_status").html();
            if(allocate_status=='未分配'){
                $(this).prop('checked',false);
            }else{
                remove=new Object;
                remove.student_id=$(this).val();
                removeList[index]=remove;
            }
        });
        removeJSON=JSON.stringify(removeList);
        
        if(removeList.length>0){
            layer.confirm('确定移除？',{btn:['确定','取消']},function(){
                $.post("/php/student-dormitory-allocating.php",{action:3,data:removeJSON},function(result){
                    resultObj=JSON.parse(result);
                    //alert(result);
                    switch(resultObj.code){
                        case '2001':
                            layer.msg(resultObj.message,{icon:1,btn:['确定']},function(){
                                location.replace(location.href);
                            });
                            break;
                        case '2002':layer.msg(resultObj.message);break;
                        case '2003':layer.msg(resultObj.message);break;
                        case '2004':layer.msg(resultObj.message);break;
                        default:break;
                    }
                });
            });
        }else{
            layer.msg('未选中任何项');
        }
    }

    //移除单个
    removeStudent=function(tabel,student_id){
        removeList=new Array();
        remove=new Object;
        remove.student_id=student_id;
        removeList[0]=remove;
        removeJSON=JSON.stringify(removeList);

        allocate_status=$(tabel).parents("tr").find(".allocate_status").html();
        if(allocate_status=='未分配'){
            layer.msg('未分配,不需要移除');
        }else if(removeList.length>0){
            layer.confirm('确定移除？',{btn:['确定','取消']},function(){
                $.post("/php/student-dormitory-allocating.php",{action:3,data:removeJSON},function(result){
                    resultObj=JSON.parse(result);
                    //alert(result);
                    switch(resultObj.code){
                        case '2001':
                            layer.msg(resultObj.message,{icon:1,btn:['确定']},function(){
                                location.replace(location.href);
                            });
                            break;
                        case '2002':layer.msg(resultObj.message);break;
                        case '2003':layer.msg(resultObj.message);break;
                        case '2004':layer.msg(resultObj.message);break;
                        default:break;
                    }
                });
            });
        }else{
            layer.msg('未选中任何项');
        }
    }

    //宿舍单选
    $('#dormitory_list').find('input[type=checkbox]').bind('click', function(){  
        $('#dormitory_list').find('input[type=checkbox]').not(this).attr("checked", false);  
    }); 

    $("select").change(function(){
        var id=$(this).attr("id");
        switch(id){
            case "jump_page":
                var allocate=GetQueryString('allocate');
                allocate=allocate==null?0:allocate;
        
                var grade=GetQueryString('grade');
                grade=grade==null?0:grade;
        
                var major=GetQueryString('major');
                major=major==null?0:major;
        
                var classs=GetQueryString('class');
                classs=classs==null?0:classs;
        
                var gender=GetQueryString('gender');
                gender=gender==null?0:gender;
        
                //var page=GetQueryString('page');
                //page=page==null?0:page;
        
                var search=GetQueryString('search');
                search=search==null?'':search;
                url="./student-dormitory-allocating.php?allocate="+allocate+"&grade="+grade
                +"&major="+major+"&class="+classs+"&gender="+gender+"&page="+$(this).val()+"&search="+search;
				window.location.href=url;
            break;
            default:break;
        }
    });
});